package com.hxzy.service.Impl;

import com.hxzy.bean.Film;
import com.hxzy.bean.HotFilm;
import com.hxzy.dao.HotFilmDao;
import com.hxzy.dao.Impl.HotFilmDaoImpl;
import com.hxzy.service.FilmService;
import com.hxzy.service.HotFilmService;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class HotFilmServiceImpl implements HotFilmService {
    private static HotFilmDao dao=new HotFilmDaoImpl();
    @Override
    public int save(HotFilm hotFilm) {
        return dao.save(hotFilm);
    }

    @Override
    public int update(HotFilm hotFilm) {
        return dao.update(hotFilm);
    }

    @Override
    public int remove(Integer integer) {
        return dao.remove(integer);
    }

    @Override
    public List<HotFilm> queryAll() {
        List<Map<String, Object>> list = dao.queryAll();
        List<HotFilm> collect = list.stream().map(t -> {
            FilmService service = new FilmServiceImpl();
            int id = (int) t.get("id");
            int fid = (int) t.get("fid");
            Film film = service.findByID(fid);
            Timestamp time = (Timestamp) t.get("time");
            LocalDateTime localDateTime = time.toLocalDateTime();
            int weight = (int) t.get("weight");
            int state = (int) t.get("state");
            return new HotFilm(id, film, localDateTime, weight, state);
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public HotFilm findByID(Integer integer) {
        return dao.findByID(integer);
    }
}
